package com.sunye.rabbit_mq_1;

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class RabbitMqListenController {

    //work工作模式消费者1
    @RabbitListener(queuesToDeclare = @Queue("workRoutingKey"),concurrency = "2")//默认  持久化、非独占、不自动删除队列
    public void receiveMeg1(Map map){
        if(map !=null && !map.isEmpty()){
          String msg =  map.get("msg")==null ? "" : map.get("msg").toString();
          List list = (List) map.get("data");
          Integer number =  map.get("number")==null ? 10 : Integer.parseInt(map.get("number").toString());
          number--;
          System.out.println("消费者1库存减少后数量：" + number);
          System.out.println("msg = " + msg);
          System.out.println("list = " + list);
        }
    }

    //work工作模式消费者2
    @RabbitListener(queuesToDeclare = @Queue("workRoutingKey"))
    public void receiveMeg2(Map map){
        if(map !=null && !map.isEmpty()){
            String msg =  map.get("msg")==null ? "" : map.get("msg").toString();
            List list = (List) map.get("data");
            Integer number =  map.get("number")==null ? 0 : Integer.parseInt(map.get("number").toString());
            number--;
            System.out.println("消费者2库存减少后数量：" + number);
//            System.out.println("msg = " + msg);
//            System.out.println("list = " + list);
        }
    }


    //fanout广播模式消费者1
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(""),//不指定名字创建临时队列
                    exchange = @Exchange(value = "fanout.exchange",type = "fanout")//绑定的交换器和类型
            )
    })
    public void fanoutMsg(Map map){
        if(map !=null && !map.isEmpty()){
            String msg =  map.get("msg")==null ? "" : map.get("msg").toString();
            Integer number =  map.get("number")==null ? 0 : Integer.parseInt(map.get("number").toString());
            number--;
            System.out.println("fanout消费者1库存减少后数量：" + number);
            System.out.println("msg = " + msg);
        }
    }

    //fanout广播模式消费者2
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,//不指定名字创建临时队列
                    exchange = @Exchange(value = "fanout.exchange",type = "fanout")//绑定的交换器和类型
            )
    })
    public void fanoutMsg2(Map map){
        if(map !=null && !map.isEmpty()){
            String msg =  map.get("msg")==null ? "" : map.get("msg").toString();
            Integer number =  map.get("number")==null ? 0 : Integer.parseInt(map.get("number").toString());
            number--;
            System.out.println("fanout消费者2库存减少后数量：" + number);
            System.out.println("msg = " + msg);
        }
    }




    //route路由模式消费者1(常用)
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(value = "routeMsg1", durable = "true"),//指定名字创建指定的队列   durable交换器和队列持久化属性为durable
                    exchange = @Exchange(value = "route.exchange",type = "direct"),//绑定的交换器和类型
                    key = {"routeInforKey","routeErrorKey"}//绑定的路由
            )
    })
    public void routeMsg1(Map map){//入参是什么就用什么参数接收
        if(map !=null && !map.isEmpty()){
            String msg =  map.get("msg")==null ? "" : map.get("msg").toString();
            Integer number =  map.get("number")==null ? 0 : Integer.parseInt(map.get("number").toString());
            number--;
            System.out.println("route消费者1库存减少后数量：" + number);
            System.out.println("msg = " + msg);
        }
    }


    //route路由模式消费者2
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,//不指定名字创建临时队列
                    exchange = @Exchange(value = "route.exchange",type = "direct"),//绑定的交换器和类型
                    key = {"routeErrorKey"} //绑定的路由
            )
    })
    public void routeMsg2(Map map){
        if(map !=null && !map.isEmpty()){
            String msg =  map.get("msg")==null ? "" : map.get("msg").toString();
            Integer number =  map.get("number")==null ? 0 : Integer.parseInt(map.get("number").toString());
            number--;
            System.out.println("route消费者2库存减少后数量：" + number);
            System.out.println("msg = " + msg);
        }
    }




    // *只能匹配一个单词
    // #能匹配多个单词
    //topic动态路由模式消费者1
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,//创建临时队列
                    exchange = @Exchange(value = "topic.exchange",type ="topic"),//绑定交换机和类型
                    key = {"user.*"}
            )
    })
    public void topicMsg1(Map map){
        if(map !=null && !map.isEmpty()){
            String msg =  map.get("msg")==null ? "" : map.get("msg").toString();
            Integer number =  map.get("number")==null ? 0 : Integer.parseInt(map.get("number").toString());
            number--;
            System.out.println("topic消费者1库存减少后数量：" + number);
            System.out.println("msg = " + msg);
        }
    }



    //topic动态路由模式消费者2
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,//创建临时队列
                    exchange = @Exchange(value = "topic.exchange",type ="topic"),//绑定交换机和类型
                    key = {"order.#","product.#","user.#"}//#能匹配多层 *只能匹配一层
            )
    })
    public void topicMsg2(Map map){
        if(map !=null && !map.isEmpty()){
            String msg =  map.get("msg")==null ? "" : map.get("msg").toString();
            Integer number =  map.get("number")==null ? 0 : Integer.parseInt(map.get("number").toString());
            number--;
            System.out.println("topic消费者2库存减少后数量：" + number);
            System.out.println("msg = " + msg);
        }
    }


}
